package ru.averta.notes.dao.impl;

import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.hibernate.search.FullTextSession;
import org.hibernate.search.Search;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import ru.averta.notes.dao.TagConnectorDao;
import ru.averta.notes.domain.Site;
import ru.averta.notes.domain.Tag;
import ru.averta.notes.domain.TagConnector;
import ru.averta.notes.domain.User;

import javax.annotation.PostConstruct;
import java.util.Collection;

@Repository
public class TagConnectorDaoImpl extends GenericDaoHibernateImpl<TagConnector, Long> implements TagConnectorDao {

    private static TagConnectorDao dao;

    @Autowired private SessionFactory sessionFactory;

    @PostConstruct
    private void setUp() {
        dao = this;
    }

    public static TagConnectorDao getDao() {
        return dao;
    }

    @Override
    public Collection<TagConnector> findByObject(Site object) {

        return createCriteria() //.add(Restrictions.eq("objectClass", objectClass))
                .add(Restrictions.eq("baseSubject.id", object.getId()))
                .list();
    }

    @Override
    public TagConnector findByObjectAndTag(Site object, Tag tag) {

        return (TagConnector) createCriteria()
                .add(Restrictions.eq("baseSubject.id", object.getId()))
                .add(Restrictions.eq("tag.id", tag.getId()))
                .uniqueResult();
    }

    @Override
    public Collection<TagConnector> findByUser(User user) {
        return createCriteria()
                .add(Restrictions.eq("userId", user.getId()))
                .list();
    }

}
